package client.net.handler;

import client.Client;
import client.game.Game;
import java.nio.ByteBuffer;
import shared.util.PacketIO;

/**
 * Handles the packet that starts a game.
 *
 * @author Vincent Tsuei
 * @version 2012-11-28
 */
public class GameStartHandler implements ClientPacketHandler {

    @Override
    public int processPacket(Client client, ByteBuffer b) {
        long gameID = b.getLong();
        String opponent = PacketIO.readString(b);
        boolean clientsTurn = b.get() == 1;
        int[] piles = new int[b.getInt()];
        for (int i = 0; i < piles.length; i++) {
            piles[i] = b.getInt();
        }
        Game g = new Game(gameID, opponent, piles, client, clientsTurn);
        
        
        
        client.registerGame(g);
        client.getUserIO().write("", false);
        client.getUserIO().write("The game has started! ", false);
        if (clientsTurn) {
            client.getUserIO().write("It is your move first.", true);
        } else {
            client.getUserIO().write("The opponent goes first.", true);
        }
        client.getUserIO().PrintGameStats();
        client.interruptIOThread(clientsTurn);
        return 0;
    }
}
